import { createApp } from 'vue';
import App from './App.vue';
import '@/assets/less/index.less';
import router from './router';
// 全局引入element-plus
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import zhCn from 'element-plus/es/locale/lang/zh-cn'; // 导入中文语言包
import * as ElementPlusIconsVue from '@element-plus/icons-vue';// 引入ico，提供ico组件
import { createPinia } from 'pinia';// 引入pinia，退供托管
// import "@/api/mock.js";// 引入moke.js,提供拦截
// import api from '@/api/api'
import { useAllDataStore } from "@/stores"

// 检查路由是否存在
function isRoute(to) {
    const routes = router.getRoutes();
    return routes.some(route => route.path === to.path);
}

// 创建 pinia 实例
const pinia = createPinia();
const app = createApp(App);

// 使用 pinia
app.use(pinia);

// 获取 store 实例
const store = useAllDataStore();

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.path !== "/login" && !store.state.token) {
        next({ name: "login" });
        return;
    }
    if (!isRoute(to)) {
        next({ name: "404" });
        return;
    }
    next();
});

// 添加菜单
store.addMenu(router, "refresh");

// 使用插件
app.use(ElementPlus, { locale: zhCn });
app.use(router);

// 注册 Element Plus 图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}

// 挂载应用
app.mount('#app');